goods_info = {'F01': {'name': '苹果', 'price': '5.5'}, 'F02': {'name': '香蕉', 'price': '2.5'},
              'F03': {'name': '葡萄', 'price': '4.5'},
              'F04': {'name': '西瓜', 'price': '1.5'}, 'F05': {'name': '香梨', 'price': '3'},
              'V01': {'name': '胡萝卜', 'price': '1.2'}, 'V02': {'name': '白菜', 'price': '0.8'},
              'V03': {'name': '西红柿', 'price': '2.2'},
              'M01': {'name': '牛肉', 'price': '45'}, 'M02': {'name': '羊肉', 'price': '50'},
              'M03': {'name': '猪肉', 'price': '25'},
              'M04': {'name': '鱼', 'price': '15'}, 'M05': {'name': '鸡蛋', 'price': '2.5'}}
shopping_cart = {}
user_info = {'swy':{'密码':'123456', '余额': '200'}}


def enter():
    global user_name, pwd
    user_name = input("请输入用户名：")
    pwd = input("请输入密码：")
    if user_name in user_info and pwd == user_info[user_name]['密码']:
        print("登录成功！")
        print("余额：", user_info[user_name]['余额'])
        return shopping_list()
    else:
        print("输入用户和密码错误或不存在，请重新输入！")
        return enter()


# 注册操作
def register():
    user_name = input("请输入需要注册用户名：")
    pwd = input("请输入注册密码：")
    money = input("请输入要充值的金额：")
    user_info.update({user_name: {'密码':pwd, '余额':money}})
    print("注册成功，请登录！！")
    return enter()


def shop():
    global sum_price
    print("祝您购物愉快！按q结账".center(50, "-"))
    while (True):
        number = input("请输入要购买商品的编号：")
        if number == "q":
            money2 = float(user_info[user_name]['余额']) - sum_price
            print("结账成功！购物清单如下：")
            print(shopping_cart)
            print("已支付：", sum_price)
            print("余额：", money2, "元")
            print("欢迎下次再来！".center(50, "-"))
            break

        elif number in goods_info.keys():
            amount = input("请输入要购买商品的数量：")
            if amount.isdigit():
                amount = float(amount)
                sum_price = 0
                # 再次购买相同商品时
                if goods_info[number]['name'] in shopping_cart.keys():
                    total_prices = float(goods_info[number]['price']) * amount
                    shopping_cart.update({goods_info[number]['name']: {'单价': goods_info[number]['price'], '数量': float(
                        shopping_cart[goods_info[number]['name']]['数量']) + amount, '总价': float(
                        shopping_cart[goods_info[number]['name']]['总价']) + total_prices}})
                else:
                    total_prices = float(goods_info[number]['price']) * amount
                    shopping_cart.update({goods_info[number]['name']: {'单价': goods_info[number]['price'], '数量': amount,
                                                                       '总价': total_prices}})
                print("购物车：", shopping_cart)
                for i in shopping_cart.values():
                    sum_price = sum_price + float(i['总价'])
                print("需要支付：",sum_price, "元")
                money2 = float(user_info[user_name]['余额'])
                print("余额：", money2, "元")
                if money2-sum_price <= 0:
                    print("余额已经不足！按1充值，按2退出")
                    option = input("请输入你的操作：")
                    if option == '1':
                        return charge()
                    elif option == '2':
                        print("退出！")
                        break
                    else:
                        print("输入错误，重新输入")
                        return shop()
                print("".center(150, "-"))
            else:
                print("输入数量有误！")
                return shop()
        else:
            print("输入编号不存在，请重新输入！")
            return shop()

# 充值金额
def charge():
    charge_money = float(input("请输入要充值的金额："))
    user_info[user_name]['余额'] = float(user_info[user_name]['余额'])+charge_money
    print("充值成功！")
    print("余额为：",user_info[user_name]['余额'])
    return shop()


def shopping_list():
    with open("shopping_info.txt", "r+", encoding="utf-8") as f:
        print(f.read())
    return shop()


def menu():
    print("欢迎来到三乐购物系统".center(100, "-"))
    print("1.登录")
    print("2.注册")
    print("3.退出")
    option = input("请输入你要执行的操作：")
    if option == "1":
        return enter()
    elif option == "2":
        return register()
    elif option == "3":
        print("退出")
    else:
        print("输入不正确，请重新输入！")
        return menu()


menu()
